package com.neiquan.backstage.platform_admin_user.web.action;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neiquan.backstage.common.util.BackendErrorCode;
import com.neiquan.backstage.common.util.CommonResp;
import com.neiquan.backstage.common.util.Sessions;
import com.neiquan.backstage.platform_admin_user.PlatformAdminUserService;
import com.neiquan.backstage.pub.bsc.dao.po.PlatformAdminUser;

@Controller
@RequestMapping(value = "platform", produces = { "text/json;charset=UTF-8" })
public class PlatformLoginAction {
	@Autowired
	private PlatformAdminUserService adminUserService;

	@ResponseBody
	@RequestMapping(value = "login")
	public String login(String account, String password, HttpSession session) {
		PlatformAdminUser user = adminUserService.queryByAccount(account);
		if (null == user) {
			return CommonResp.buildErrorResp(BackendErrorCode.account_not_exist).toJsonString();
		}
		if (!user.getPassword().equals(password)) {
			return CommonResp.buildErrorResp(BackendErrorCode.account_password_not_match).toJsonString();
		}
		Sessions.setLoginedUser(user, session);
		return CommonResp.buildSuccessResp().toJsonString();
	}

	@ResponseBody
	@RequestMapping(value = "logout")
	public String logout(HttpSession session) {
		Sessions.removeLoginedUser(session);
		return CommonResp.buildSuccessResp().toJsonString();
	}
}
